import { getBaseUrl } from '@/services/urlconst';
import { useRequest } from 'umi';
import { requestFunHtml } from '@/utils';
import { AssetListManageTab } from '../mconfig/ManageTabSwitch';

const baseUrl = getBaseUrl();
// const baseUrl = '/wenchuang';

/**
 * 资产列表
 * @param curRes
 */
const exportAssetService = (curRes: any, callback: (res: any) => void) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = AssetListManageTab(curRes);

  if (queryTypeObj?.params == 'assetDepartmentID') {
    queryTypeObj.assetListFlag = 1;
  } else {
    delete queryTypeObj?.['assetListFlag'];
  }

  const exportAsset = useRequest(
    (payload: any, opt: any) => {
      let bodyParams = {
        ...payload,
        order: 'desc',
        sort: 'createTime',
        ...queryTypeObj,
        ...opt,
      };

      if (bodyParams?.useTime) {
        const [useTimeStart, useTimeEnd] = bodyParams.useTime;
        bodyParams['useTimeStart'] = useTimeStart.format('YYYY-MM-DD 00:00:00');
        bodyParams['useTimeEnd'] = useTimeEnd.format('YYYY-MM-DD 23:59:59');
        delete bodyParams['useTime'];
      }

      if (bodyParams?.enterStoreTime) {
        const [enterStoreTimeStart, enterStoreTimeEnd] = bodyParams.enterStoreTime;
        bodyParams['enterStoreTimeStart'] = enterStoreTimeStart.format('YYYY-MM-DD 00:00:00');
        bodyParams['enterStoreTimeEnd'] = enterStoreTimeEnd.format('YYYY-MM-DD 23:59:59');
        delete bodyParams['enterStoreTime'];
      }

      delete bodyParams['useDepartmentFormNameDept'];
      delete bodyParams['useDepartmentFormNameUser'];
      if (bodyParams['userID']?.length) {
        bodyParams['userID'] = bodyParams['userID']?.toString();
      } else {
        delete bodyParams['userID'];
      }
      let reqUrl = `${baseUrl}/assetInfo/export`;
      return requestFunHtml('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    exportAssetRun: exportAsset?.run,
    exportAssetLoading: exportAsset?.loading,
  };
};

/**
 * 预算一体化资产列表
 * @param curRes
 */
const exportAssetBudgetExportService = (callback: (res: any) => void) => {
  const exportAssetBudgetExport = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        order: 'desc',
        sort: 'createTime',
      };
      let reqUrl = `${baseUrl}/assetInfo/budgetExport`;
      return requestFunHtml('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    exportAssetBudgetExportRun: exportAssetBudgetExport?.run,
    exportAssetBudgetExportLoading: exportAssetBudgetExport?.loading,
  };
};

export { exportAssetService, exportAssetBudgetExportService };
